<template>
<div>
    <TabsComponent
            :tabs="tabsOne"
            :current="tabIndex"
            v-on:tab-switch="switchHandler"/>
    <!--type:'',//0 是不需要关联 从详情页过来 1 是需要关联 详情页过来 2 是可以报修从设备查询页过来-->

    <div v-if="type=='1'||type=='2'" >
        <yd-button class="repairbutton-backgrond" type="hollow" @click.native="gotorepair">{{buttonName}}</yd-button>

    </div>
    <!-- 路由出口 -->
    <!-- 路由匹配到的组件将渲染在这里 -->
    <router-view></router-view>
</div>


</template>

<script>
    import {machinemachineLink,machineCodeDetails} from "../../pcof/post";
    import {userInfoData,qn_domain} from '../../globalVariables'
    import TabsComponent from "@/components/TabsComponent.vue";

    export default {
        name: "equipmentDetailrecord",
        data() {
            return {
                buttonName:'',
                orderUuid:'',//报修的单子
                equipmentuuid: '',
                type:'',//0 是不需要关联 从详情页过来 1 是需要关联 详情页过来 2 是可以报修从设备查询页过来
                equipmentDetail:{},
                tabsOne: [
                    {id:1, text:"设备信息"},
                    {id:2, text:"维修记录"},
                ],
                tabIndex: 1,

            }

        },

        mounted() {
            let that = this;
            // 添加返回事件监听
            window.addEventListener("popstate", function(e) {
                // console.log('哈哈哈斤斤计较');
                //  let that = this;
                //
                // that.$dialog.toast({
                //     mes: '哈哈哈',
                //     timeout: 1500
                // });
            //     that.$dialog.confirm({
            //         title: '选填标题',
            //         mes: '我有一个小毛驴我从来也不骑！',
            //         opts: () => {
            //             this.$dialog.toast({mes: '你点了确定', timeout: 1000});
            //         }
            //     });
             }, false);
        },

        methods: {
            switchHandler(payload) {
                if (payload.index === 0) {

                    this.$router.replace({name: 'equipmentDetrail', params: {equipmentuuid: this.equipmentuuid}});
                } else if (payload.index === 1) {
                    this.$router.replace({name: 'equipmentRecord', params: {equipmentuuid: this.equipmentuuid}});
                }
            },
           async gotorepair()
            {
                //0 是不需要关联 从详情页过来 1 是需要关联 详情页过来 2 是可以报修从设备查询页过来
                if (this.type ==='0')
                {

                }else if (this.type ==='1')
                {

                  this.getguanlian();
                }else if (this.type ==='2')
                {
                    this.baoxiu();
                }
            },
            async getguanlian()
            {
                /**
                 * 关联设备
                 * orderUuid 报修单id
                 * machineUuid	String 设备id
                 */
                    // export const machinemachineLink = (
                    // mobile,orderUuid,machineUuid,
                var mobile = JSON.parse(localStorage.getItem(userInfoData)).mobile;
                this.$dialog.loading.open('加载中...');
                var response = await machinemachineLink(mobile,this.orderUuid,this.equipmentuuid);
                if (response.data.code === 200) {
                    this.$dialog.loading.close();
                    this.$router.back(-1);
                }
                else {
                    this.$dialog.loading.close();
                    this.$dialog.toast({
                        mes: response.data.msg,
                        timeout: 1500
                    });
                }
            },
            async baoxiu()
            {
                this.equipUuid = this.$route.params.equipmentuuid;
                var mobile = JSON.parse(localStorage.getItem(userInfoData)).mobile;
                this.$dialog.loading.open('加载中...');
                var response = await machineCodeDetails(mobile,'',this.equipUuid);
                if (response.data.code === 200) {
                    this.$dialog.loading.close();
                    this.equipmentDetail['typeName']=response.data.data.typeName;
                    this.equipmentDetail['machineName']=response.data.data.machineName;
                    this.equipmentDetail['bussinessName']=response.data.data.bussinessName;
                    this.equipmentDetail['name']=response.data.data.name;
                    this.equipmentDetail['machineUuid']=this.equipmentuuid;
                    this.equipmentDetail['instName']=response.data.data.instName;
                    this.equipmentDetail['instUuid']=response.data.data.instUuid;
                    this.$router.replace({name: 'repairEquipment', params: {equipmentDetail: this.equipmentDetail}});

                }
                else
                {
                    this.$dialog.loading.close();
                    this.$dialog.toast({
                        mes: response.data.msg,
                        timeout: 1500
                    });
                }
            },

        },
        beforeRouteLeave(to,from,next) {
            if ( to.path === '/repair_equipment' ) {
                next();
            } else {
                if (this.type ==='0')
                {
                    next();

                }else if (this.type ==='1')
                {
                 //关联';
                    let that = this;

                    weui.confirm('是否将该设备关联到该报修单？', {
                        title: '',
                        buttons: [{
                            label: '关联',
                            type: 'primary',
                            onClick: function () {
                                that.getguanlian();
                                next();
                            }
                        }, {
                            label: '取消',
                            type: 'default',
                            onClick: function () {
                                console.log('yes');
                                next();
                            }
                        }]
                    });

                }else if (this.type ==='2')
                {
                  //报修';
                     let that = this;

                    weui.confirm('是否将该设备报修？', {
                        title: '',
                        buttons: [{
                            label: '是',
                            type: 'primary',
                            onClick: function () {
                                console.log('no');
                                that.baoxiu();
                                next();
                            }
                        }, {
                            label: '否',
                            type: 'default',
                            onClick: function () {
                                console.log('yes');
                                next();
                            }
                        }]
                    });

                }

            }
        },
        created() {
            //接受上个页面传值 在实例创建完成后被立即调用
            this.equipmentuuid = this.$route.params.equipmentuuid;
            this.type= this.$route.params.type;
            this.orderUuid= this.$route.params.orderUuid;
           //0 是不需要关联 从详情页过来 1 是需要关联 详情页过来 2 是可以报修从设备查询页过来

            if (this.type ==='0')
            {

            }else if (this.type ==='1')
            {
                this.buttonName='关联';

            }else if (this.type ==='2')
            {
                this.buttonName='报修';

            }

            this.$router.replace({name: 'equipmentDetrail', params: {equipmentuuid: this.equipmentuuid}});


       },
        components: {

            TabsComponent,
        },
    }
</script>

<style scoped>
 .repairbutton-backgrond
 {
     top: 0.2rem;
     right: 0.1rem;
     position: absolute;
     margin-right: 10px;
     height: 0.5rem;
     width: 0.7rem;
     background-color: #eaf4f4;
     color: #3cc8a8;
     border: 1px solid #3cc8a8;
 }


</style>